import sys
import time
import pygame

# 初始化
pygame.init()

# 元素移动函数
def Move(O, end: tuple, speed: float = 0.01, Step: int = 1):
    '''
    :param O: 移动对象
    :param end: 终点位置（x, y)
    :param speed: 移动1个步长的速度, 默认0.01s
    :return: 移动后位置
    '''
    # 对象初始x, y
    x, y = O.topleft
    # 对象末位置
    end_x, end_y = end
    time.sleep(speed)
    if x == end_x and y == end_y:
        return True
    elif x < end_x:
        O.x += Step
    elif x > end_x:
        O.x -= Step
    elif y < end_y:
        O.y += Step
    elif y > end_y:
        O.y -= Step
    return 'err'



screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("少帅下飞机")
# 初始化字体
font = pygame.font.SysFont("SimHei", 30)

feiji = pygame.image.load("feiji.png") # 飞机对象
feiji_rect = feiji.get_rect() # 飞机矩形对象
print(feiji_rect)
shaoshuai = font.render('少帅', True, (0,0,0))  # 少帅对象
shaoshuai_rect = shaoshuai.get_rect() # 少帅矩形对象
print(shaoshuai_rect)
weibing = font.render('卫兵', True, (0,0,0) ) # 卫兵1对象
weibing_rect = weibing.get_rect() # 卫兵1矩形对象
print(weibing_rect)
weibing2 = font.render('卫兵', True, (0,0,0) ) # 卫兵2对象
weibing_rect2 = weibing.get_rect() # 卫兵2矩形对象
print(weibing_rect2)


# 飞机初始位置
feiji_rect.x, feiji_rect.y = (0, 0)
# 少帅初始位置
shaoshuai_rect.x, shaoshuai_rect.y = (320, 65)
# 卫兵1初始位置
weibing_rect.x, weibing_rect.y = (320 - 65 , 65)

# 卫兵2初始位置
weibing_rect2.x, weibing_rect2.y = (320 + 65 , 65)


# 飞机末位置
feiji_end_postion = (320, 0)
# 少帅末位置
shaoshuai_end_postion = (320, 480)
# 卫兵1末位置
weibing_end_postion = (320 - 65, 480)
# 卫兵2末位置
weibing2_end_postion = (320 + 65, 480)


# 飞机移动速度
feiji_speed = 0.1
# 少帅移动速度
shaoshuai_speed = 0.1
# 卫兵移动速度
weibing_speed = 0.1
end = False


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    screen.fill((255, 255, 255))
    # 绘制对象

    screen.blit(weibing, weibing_rect)
    screen.blit(weibing2, weibing_rect2)
    screen.blit(feiji, feiji_rect)
    # 飞机移动
    feiji_move_status = Move(feiji_rect, feiji_end_postion, feiji_speed, 5)
    if feiji_move_status is True:
        screen.blit(shaoshuai, shaoshuai_rect)
        Move(shaoshuai_rect, shaoshuai_end_postion, shaoshuai_speed, 10)
        Move(weibing_rect, weibing_end_postion, weibing_speed, 10)
        Move(weibing_rect2, weibing2_end_postion, weibing_speed, 10)
        pass
    pygame.display.flip()

